package cn.com.gzheroli.core.util;

import java.util.ArrayList;
import java.util.List;

public class NameUtils {
	/**
	 * convert camel name to underscore name
	 * @return
	 */
	public static String camel2underscore(String camelName){
		//先把第一个字母大写
		camelName = capitalize(camelName);
		
		String regex = "([A-Z][[a-z][0-9]]+)";
		 regex="(\\B[A-Z])";
		String replacement = "_$1";
		//把大写字母前加下划线
		String underscoreName = camelName.replaceAll(regex, replacement);
		//output: Pur_Order_Id_ 接下来把最后一个_去掉，然后全部改小写
		if(underscoreName.trim().endsWith("_"))
			underscoreName = underscoreName.toLowerCase().substring(0, underscoreName.length()-1);
		else
			underscoreName = underscoreName.toLowerCase().substring(0, underscoreName.length());
		
		return underscoreName;
	}
	
	/**
	 * convert underscore name to camel name
	 * @param underscoreName
	 * @return
	 */
	public static String underscore2camel(String underscoreName){
		String[] sections = underscoreName.split("_");
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<sections.length;i++){
			String s = sections[i].toLowerCase();
			if(i==0){
				sb.append(s);
			}else{
				sb.append(capitalize(s));
			}
		}
		return sb.toString();
	}
	
	/**
	 * capitalize the first character
	 * @param str
	 * @return
	 */
	public static String capitalize(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return str;
        }
        return new StringBuilder(strLen)
            .append(Character.toTitleCase(str.charAt(0)))
            .append(str.substring(1))
            .toString();
    }
	
	public static String underscoreName(String name)  
	 {
	   StringBuilder result = new StringBuilder();  
	   if ((name != null) && (name.length() > 0)) {  
	     result.append(name.substring(0, 1).toLowerCase());  
	     for (int i = 1; i < name.length(); ++i) {  
	       String s = name.substring(i, i + 1);  
	       if (s.equals(s.toUpperCase())&&!s.equals("_")) {
	         result.append("_");
	         result.append(s.toLowerCase());  
	       }else {  
	         result.append(s);  
	       }  
	     }  
	   }  
	   return result.toString();  
	 }
	public static String trans(String str) {  
        List record = new ArrayList();  
        for (int i = 0; i < str.length(); i++) {  
            char tmp = str.charAt(i);  
            if ((tmp <= 'Z') && (tmp >= 'A')) {  
                record.add(i);// 记录每个大写字母的位置  
            }  
        }  
        if (null == record || record.size() == 0) {  
            return str;  
        }  
        str = str.toLowerCase();  
        char[] charofstr = str.toCharArray();  
        String[] t = new String[record.size()];  
        for (int i = 0; i < record.size(); i++) {  
            t[i] = "_" + charofstr[(Integer) record.get(i)];  
        }  
        String result = "";  
        int flag = 0;  
        for (int i = 0; i < str.length(); i++) {  
            if ((flag < record.size()) && (i == (Integer) record.get(flag))) {  
                result += t[flag];  
                flag++;  
            } else  
                result += charofstr[i];  
        }  
        return result;  
    } 
	public static void main(String []args){
		String str="ID";
		System.out.println(Character.toTitleCase(str.charAt(0)));
		System.out.println(new StringBuilder(str.length())
        .append(Character.toTitleCase(str.charAt(0)))
        .append(str.substring(1))
        .toString());
		System.out.println(NameUtils.underscore2camel("depart_id"));
		System.out.println(NameUtils.camel2underscore("wgs84X"));
	}
}
